package com.ydche.service.impl;

import com.ydche.dao.ICustomerDao;

import com.ydche.idl_output.CustomerInfo;
import com.ydche.idl_output.CustomerInfoDB;
import com.ydche.service.ICustomerService;
import com.ydche.util.MyTime;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private ICustomerDao customerDao;
    private static Logger log = LogManager.getLogger(CustomerServiceImpl.class);

    @Override
    public Long addCustomer(Long userId, String mobile, String aliasName, String identityId, Long ownerId, boolean isLocked) {
        log.info("service--------add customer");
        CustomerInfoDB customerInfo = new CustomerInfoDB();
        customerInfo.setCustomer_name(aliasName);
        customerInfo.setDoc_code(identityId);
        customerInfo.setDoc_type_id(1);
        customerInfo.setIsImmevalid(1);
        customerInfo.setIs_enabled(false);
        customerInfo.setIs_locked(isLocked);
        customerInfo.setMobile(mobile);
        if(isLocked){
            customerInfo.setUser_id(userId.intValue());
            customerInfo.setOwner_id(ownerId.intValue());
        }else{
            customerInfo.setOwner_id(userId.intValue());
        }
        customerInfo.setCreate_date(MyTime.getCurrentDateTime());
        return (long)customerDao.addCustomer(customerInfo);
    }

    @Override
    public boolean updateCustomer(CustomerInfoDB customerInfo) {
        log.info("service--------update customer");
        return false;
    }

    @Override
    public Long saveCustomer(Long ownerId, String mobile, String aliasName, String identityId) {
        List<CustomerInfoDB> customerInfoDBList = customerDao.getCustomerInfo(ownerId, mobile, identityId, aliasName);
        for(CustomerInfoDB customerInfoDB: customerInfoDBList){
            if(!customerInfoDB.is_locked){
                return (long)customerInfoDB.getId();
            }
        }
        return addCustomer(null, mobile, aliasName, identityId, ownerId, false);
    }


}
